package com.saceem.imagenes.qr;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Hashtable;

import javax.imageio.ImageIO;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.ChecksumException;
import com.google.zxing.FormatException;
import com.google.zxing.LuminanceSource;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.NotFoundException;
import com.google.zxing.Reader;
import com.google.zxing.Result;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.HybridBinarizer;
import com.google.zxing.oned.Code128Writer;

public class GenerateBarCode {

	public void readBarCode() {
		InputStream barCodeInputStream;
		try {
			barCodeInputStream = new FileInputStream("file.jpg");

			BufferedImage barCodeBufferedImage = ImageIO
					.read(barCodeInputStream);

			LuminanceSource source = new BufferedImageLuminanceSource(
					barCodeBufferedImage);
			BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
			Reader reader = new MultiFormatReader();
			Result result;
			result = reader.decode(bitmap);

			System.out.println("Barcode text is " + result.getText());
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ChecksumException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (FormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public static void writeBarCode() {
		String IMG_PATH = "C:/Users/Public/Pictures/Sample Pictures/Chelo/";
		String text = "0103243NICO"; // this is the text that we want to encode

		// int width = 400;
		// int height = 200;
		// change the height and width as per your requirement

		int width = 132; // 224; //132
		int height = 46; // 145; //46

		// (ImageIO.getWriterFormatNames() returns a list of supported formats)
		String imageFormat = "png"; // could be "gif", "tiff", "jpeg"

		BitMatrix bitMatrix;
		try {

			// Hashtable param = new Hashtable();
			// param.put(text, text);

			// bitMatrix = new Code128Writer().encode(text,
			// BarcodeFormat.CODE_128, width, height);
			//
			// MatrixToImageWriter.writeToStream(bitMatrix, imageFormat,
			// new FileOutputStream(new File(IMG_PATH + text + ".png")));

			for (int i = 0; i < 10; i++) {

				if (i < 6) {
					String code = "0101243450" + i;
					System.out.println(code);
					// bitMatrix = new Code128Writer().encode(code,
					// BarcodeFormat.CODE_128, width, height);

					Hashtable<String, String> param = new Hashtable<String, String>();
					param.put(code, code);
					bitMatrix = new Code128Writer().encode(code,
							BarcodeFormat.CODE_128, width, height, param);

					MatrixToImageWriter.writeToStream(bitMatrix, imageFormat,
							new FileOutputStream(new File(IMG_PATH + code
									+ ".png")));

				} else {
					String code = "0601243450" + i;
					System.out.println(code);
					bitMatrix = new Code128Writer().encode(code,
							BarcodeFormat.CODE_128, width, height);

					MatrixToImageWriter.writeToStream(bitMatrix, imageFormat,
							new FileOutputStream(new File(IMG_PATH + code
									+ ".png")));

				}
			}

		} catch (WriterException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public static void main(String[] args) {
		writeBarCode();
	}
}
